package com.common.system;

import java.util.List;

import com.common.dto.LoginAccount;
import com.common.dto.Proxy;
import com.common.dto.SnatchItem;
import com.common.dto.WebConfig;
import com.common.dto.WebNode;
import com.common.dto.WebUrl;

/**
 * 系统内存数据
 * 
 * @author Administrator
 * 
 */
public class SystemData {
	public static final boolean TEST = false;

	static public WebConfig webConfig = new WebConfig(); // 节点配置信息

	public static WebNode findWebNode(String webCode) {
		List<WebNode> list = webConfig.getWebNodes();
		for (WebNode webNode : list) {
			if (webNode.getWebCode().equals(webCode)) {
				return webNode;
			}
		}
		return null;
	}
	
	
	public static LoginAccount findLoginAccount(String webCode, String userName) {
		WebNode webNode = findWebNode(webCode);
		if (null == webNode)
			return null;
		List<LoginAccount> loginAccounts = webNode.getLoginAccounts();
		for (LoginAccount loginAccount : loginAccounts) {
			if (userName.equals(loginAccount.getUserName())) {
				return loginAccount;
			}
		}
		return null;
	}
	
	
	public static WebUrl findWebUrl(String webCode, String url) {
		WebNode webNode = findWebNode(webCode);
		if (null == webNode)
			return null;
		List<WebUrl> webUrls = webNode.getWebUrls();
		for (WebUrl webUrl : webUrls) {
			if (url.equals(webUrl.getUrl())) {
				return webUrl;
			}
		}
		return null;
	}
	
	

	public static Proxy findProxy(String webCode, String ip) {
		WebNode webNode = findWebNode(webCode);
		if (null == webNode)
			return null;
		List<Proxy> proxys = webNode.getProxys();
		for (Proxy proxy : proxys) {
			if (ip.equals(proxy.getIp())) {
				return proxy;
			}
		}
		return null;
	}
	
	

	public static SnatchItem findSnatchItem(String webCode, String itemOnlyCode) {
		WebNode webNode = findWebNode(webCode);
		if (null == webNode)
			return null;
		List<SnatchItem> snatchItems = webNode.getSnatchItems();
		for (SnatchItem snatchItem : snatchItems) {
			if (itemOnlyCode.equals(snatchItem.getItemOnlyCode())) {
				return snatchItem;
			}
		}
		return null;
	}
	
	

	
	
	public static void addWebNode(WebNode webNode){
		webConfig.getWebNodes().add(webNode);
	}
	
	
	public static void addLoginAccount(String webCode,LoginAccount loginAccount){
		WebNode webNode = findWebNode(webCode);
		if(null != webNode){
			webNode.getLoginAccounts().add(loginAccount);
		}
	}
	
	public static void addSnatchItem(String webCode,SnatchItem snatchItem){
		WebNode webNode = findWebNode(webCode);
		if(null != webNode){
			webNode.getSnatchItems().add(snatchItem);
		}
	}
	
	public static void addWebUrl(String webCode,WebUrl webUrl){
		WebNode webNode = findWebNode(webCode);
		if(null != webNode){
			webNode.getWebUrls().add(webUrl);
		}
	}
	
	public static void addProxy(String webCode,Proxy proxy){
		WebNode webNode = findWebNode(webCode);
		if(null != webNode){
			webNode.getProxys().add(proxy);
		}
	}
}
